package com.maxatime.web.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.maxatime.common.constant.BusinessContents;
import com.maxatime.dao.CodeMapper;
import com.maxatime.pojo.entity.Code;
import com.maxatime.pojo.entity.EmployeeInfo;
import com.maxatime.pojo.entity.EmployeeRegister;
import com.maxatime.service.CodeService;
import com.maxatime.service.LogonService;
import com.maxatime.util.MyContent;
import com.maxatime.util.ValidateCodeUtils;


@RequestMapping("/Logon")
@Controller
public class LogonController {

	Logger log = LogManager.getLogger();
	
	@Autowired
	LogonService logonService;
	
	@Autowired
	CodeMapper codeMapper;
	
	@Autowired
	CodeService codeService;
	
	@Autowired
    private JavaMailSender mailSender;
	@RequestMapping("/logonPage")
	public String logonPage() {
		
		return "logon/logon";
	}
	
	@RequestMapping("/logon")
	@ResponseBody 
	public Map<String,Object> logon(@RequestBody String strReq,HttpSession session){
		JSONObject jsonObj = JSONObject.parseObject(strReq);
		String email = jsonObj.getString("email");
		String password = jsonObj.getString("password");
		String captcha = jsonObj.getString("captcha");
		String regex = "\\w+@\\w+(\\.\\w{2,3})*\\.\\w{2,3}";
		Map<String, Object> m = new HashMap<>();
		EmployeeRegister employeeRegister=new EmployeeRegister();
		employeeRegister.setEmail(email);
		employeeRegister.setPassword(password);
		EmployeeRegister employeeRegister1 =logonService.listData(email,password,captcha);
		
		
		if(employeeRegister1!=null) {
			
			EmployeeInfo employeeInfo=new EmployeeInfo();
			employeeInfo.setUserId(employeeRegister1.getUserId());
//			List<EmployeeInfo> list1=employeeInfoMapper.listData(employeeInfo);
//			m.put("grxx", list1.get(0));
			m.put(MyContent.CODE, "0000");
			m.put(MyContent.MSG, "登录成功");
			session.setAttribute(BusinessContents.USER_ID, "1111");
		}else if(captcha==null){
			m.put(MyContent.CODE, "9999");
			m.put(MyContent.MSG, "验证码错误，请重新输入");
		}else if(employeeRegister1==null) {
			m.put(MyContent.CODE, "2222");
			m.put(MyContent.MSG, "账号或密码错误，请重新输入");
		}
		return m;
	}
	@RequestMapping("/email")
	@ResponseBody
	public Map<String, Object> eamil(@RequestBody String strReq){
		JSONObject jsonObj = JSONObject.parseObject(strReq);
		Map <String,Object> m = new HashMap<>();
		String  email = jsonObj.getString("email");
//		String captcha = jsonObj.getString("captcha");
		String code = ValidateCodeUtils.generateValidateCode(6).toString();
		SimpleMailMessage message = new SimpleMailMessage();
		 message.setFrom("15910347212@163.com");
	     message.setTo(email);
	     message.setSubject("短信验证");
	     message.setText("您的验证码为：" + code + ",请勿泄露给他人。");
	     Date d= new Date();
	     Code code1 =new Code();
		 code1.setCaptcha(code);
		 code1.setEmail(email);
		 code1.setCreatetime(d);
	     codeService.insertSelective(code1);
	     try {
	            mailSender.send(message);
	            log.info("send success.................");
	        } catch (Exception e) {
	            log.error("异常", e);
	        }
	     m.put(MyContent.CODE, "0000");
		 m.put(MyContent.MSG, "发送成功");
		return m;
    }
	
	@RequestMapping("/loginout")
	public String loginOut(HttpServletRequest request) {
		// request.getSession().invalidate();
		request.getSession().removeAttribute(BusinessContents.STUDENT_ID);
		return "redirect:/Logon/logonPage";
	}
	
	
}
